package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.Result;
import com.chen.dto.ArticleSearchDTO;
import com.chen.mapper.ArticleMapper;
import com.chen.pojo.Article;
import com.chen.service.Impl.ArticleServiceImpl;
import com.chen.vo.ConditionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author chenchao
 */
@RestController
@RequestMapping("/article")
@Api(value = "ArticleController", tags = "文章操作")
public class ArticleController {
    @Resource
    private ArticleServiceImpl articleService;

    @Resource
    private ArticleMapper articleMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/addArticle")
    @ApiOperation("添加博客")
    public Result<?> addArticle(@RequestBody Article article) {
        return articleService.addArticle(article);
    }

    @PutMapping("/updateArticle")
    @ApiOperation("更新博客")
    public Result<?> updateArticle(@RequestBody Article article) {
        return articleService.updateNews(article);
    }


    @DeleteMapping("/deleteArticle/{id}")
    @ApiOperation("删除博客")
    public Result<?> deleteArticle(@PathVariable("id") Long id) {
        return articleService.delete(id);
    }

    @GetMapping("/query/{id}")
    @ApiOperation("查询")
    public Result<?> getById(@PathVariable("id") Long id) {
        return articleService.getById(id);
    }


    @GetMapping("/findAll")
    @ApiOperation("查询所有留言")
    public Result<?> findAll() {
        return articleService.findAll();
    }

    @GetMapping("/articleList")
    @ApiOperation("查询前五条数据")
    public Result<?> articleList() {

        return articleService.articleList();

    }

    @GetMapping("/list/{author}")
    @ApiOperation("查询属于该用户的数据")
    public Result<?> list1(@PathVariable("author") String author) {
        return articleService.list1(author);
    }

    @GetMapping("/categoryList/{author}")
    @ApiOperation("查询文章数量")
    public Result<?> categoryList(@PathVariable("author") String author) {
        return articleService.categoryList(author);
    }

    @GetMapping("/categorys/{author}/{category}")
    @ApiOperation("该作者有哪些分类")
    public Result<?> categorys(@PathVariable("author") String author, @PathVariable("category") String category) {
        return articleService.categorys(author, category);

    }

    @PutMapping("/updateCount")
    @ApiOperation("数量加一")
    public Result updateCount(@RequestBody Article article) {
        return articleService.updateCount(article);
    }

    @GetMapping("/selectArticleOne/{id}")
    @ApiOperation("查询某一篇文章锁包含的集合")
    public Result selectArticleOne(@PathVariable("id") int id) {
        return articleService.selectArticleOne(id);
    }

    @GetMapping("/selectArticleByFlag")
    @ApiOperation("查询不是加密的文章")
    public Result selectArticleByFlag() {
        return articleService.selectArticleByFlag();

    }


    @GetMapping("/seeCount")
    @ApiOperation("查询浏览量")
    public Result seeCount() {
        return articleService.seeCount();

    }

    @GetMapping("/page")
    @ApiOperation(value = "分页")
    public IPage<Article> findPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String category,
                                   @RequestParam(defaultValue = "") String tag,
                                   @RequestParam(defaultValue = "") Integer isEnable) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Article> queryWrqpper = new QueryWrapper<>();

        if (!"".equals(category)) {
            queryWrqpper.like("category", category);
        }
        if (!"".equals(tag)) {
            queryWrqpper.like("tag", tag);
        }

//        queryWrqpper.orderByDesc("id");
        IPage<Article> userPage = articleService.page(page, queryWrqpper);
        return userPage;

    }


    /**
     * 搜索文章
     *
     * @param condition 条件
     * @return {@link Result<ArticleSearchDTO>} 文章列表
     */
    @ApiOperation(value = "搜索文章")
    @GetMapping("/search")
    public Result<List<ArticleSearchDTO>> listArticlesBySearch(ConditionVO condition) {
        return Result.success(articleService.listArticlesBySearch(condition));
    }


    @GetMapping("/isEnable")
    @ApiOperation(value = "被举报文章")
    public Result isEnable() {
        return Result.success(articleService.isEnable());
    }


    @GetMapping("/articleCount")
    @ApiOperation(value = "文章数")
    public Result articleCount() {
        return Result.success(articleMapper.articleCount());
    }

@GetMapping("/lookCount")
    public Result lookCount() {
        Long views = redisTemplate.opsForValue().increment("views");
        return Result.success(views);
    }

}
